package com.xmu.makerspace.controller;

import com.xmu.makerspace.model.RegisterSubmitDTO;
import com.xmu.makerspace.model.TempMemberDTO;
import com.xmu.makerspace.model.UpdateRegisterVO;
import com.xmu.makerspace.service.UpdateRegisterService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jetbrains.annotations.NonNls;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by status200 on 2017/8/8.
 */
@RestController
@RequestMapping("/api/update-register")
public class UpdateRegisterApiController {

    @NonNls
    private static Log log = LogFactory.getLog(UpdateRegisterApiController.class);

    private final UpdateRegisterService updateRegisterService;

    @Autowired
    public UpdateRegisterApiController(UpdateRegisterService updateRegisterService) {
        this.updateRegisterService = updateRegisterService;
    }

    @GetMapping("get-info")
    public UpdateRegisterVO getInfo(@RequestParam("teamId") String teamId) {
        log.info(String.format("loading temp team \"%s\" data", teamId));

//        return genUpdateRegisterVO(teamId);
        UpdateRegisterVO vo = updateRegisterService.getTeamSubmitInfo(teamId);

        log.info(String.format("temp team \"%s\" data is loaded ", teamId) + vo);

        return vo;
    }

    @PostMapping("submit")
    public void submit(@RequestBody RegisterSubmitDTO data) {
        log.info("temp team's change is submitted but not applied, data is" + data);

        updateRegisterService.submit(data);

        log.info("temp team's change submit successfully, data is " + data);
    }

    @GetMapping("validate-team-id")
    public Map<String, Object> validateTeamId(@RequestParam("teamId") String teamId) {

        HashMap<String, Object> data = new HashMap<>();
//        data.put("exists",true);
        data.put("exists", updateRegisterService.checkIfTeamExists(teamId));

        return data;
    }

    // 调试时用的假数据
    private UpdateRegisterVO genUpdateRegisterVO(String teamId) {
        UpdateRegisterVO data = new UpdateRegisterVO();
        data.setBelongField("金融业");
        ArrayList<String> files = new ArrayList<>();
        files.add("演武创客空间申请表.docx");
        files.add("项目文档.zip");
        data.setFiles(files);
        data.setPlan("项目规划");
        data.setProjectBrief("项目描述");
        data.setProjectDirector("主营业务");
        data.setProjectName("项目名");
        data.setTeamId(teamId);
        data.setTeamName("团队名");
        data.setWorkFoundation("前期基础");
        data.setProjectType("自选");

        ArrayList<TempMemberDTO> members = new ArrayList<>();
        members.add(new TempMemberDTO("软件学院", "本科", "358941501@qq.com", 1, "24320152202752", "黄一鑫", teamId, "15392035361"));
        members.add(new TempMemberDTO("软件学院", "本科", "529930262@qq.com", 2, "24320152202755", "江子攸", teamId, "15959237237"));
        data.setMembers(members);

        return data;
    }
}
